//
//  KongWIllnessDetailViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "KongWIllnessDetailViewController.h"
#import "KongWURLSessionHandle.h"
#import "KongWHightOfLabel.h"
#import "KongWIllnessDetailModel.h"
#import "KongWIllnessDetailTableViewCell.h"
#import "KongWCellSelectDetailViewController.h"
#import <SVProgressHUD.h>
#define kHeight     [UIScreen mainScreen].bounds.size.height / 667
#define kWidth      [UIScreen mainScreen].bounds.size.width / 375
@interface KongWIllnessDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (retain, nonatomic) UITableView *table;

@property (copy, nonatomic) NSString *content;

@property (assign, nonatomic) CGFloat hight;

@property (assign, nonatomic) NSInteger page;

@property (retain, nonatomic) NSDictionary *dic;

@property (retain, nonatomic) UIView *backView;

@property (retain, nonatomic) NSMutableArray *modelArray;

@end

@implementation KongWIllnessDetailViewController
-(void)dealloc
{
    [_uid release];
    [_table release];
    [_titleStr release];
    [_content release];
    [_backView release];
    [_dic release];
    [_modelArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 30);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"KongWback"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.title = @"文章详情";
    self.view.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0];
    self.title = self.titleStr;
    self.modelArray = [NSMutableArray array];
    self.page = 0;
    [self loadData];
    
}

-(void)loadData
{
    [KongWURLSessionHandle handleGETWithUrlString:[NSString stringWithFormat:@"http://app.5ichong.com/app.php/tag/show_tag?id=%@", self.uid] parameters:nil showHuD:YES onView:self.view successfulBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        self.content = [[dic objectForKey:@"data"] objectForKey:@"content"];
        [self creatTitleView];
    } failureBlock:^(NSError *error) {
        NSLog(@"Error:%@", error);
    }];
    
    [KongWURLSessionHandle handleGETWithUrlString:[NSString stringWithFormat:@"http://app.5ichong.com/app.php/tag/get_batch_article_by_tag?id=%@&limit=20&page=%ld", self.uid, self.page] parameters:nil showHuD:YES onView:self.view successfulBlock:^(id responseObject) {
        self.dic = responseObject;
        for (NSDictionary *dic in [[self.dic objectForKey:@"data"] objectForKey:@"list"]) {
            KongWIllnessDetailModel *illnessDetailModel = [KongWIllnessDetailModel illnessDetailWithDictionary:dic];
            [self.modelArray addObject:illnessDetailModel];
        }
        
        
        [self.table reloadData];
    } failureBlock:^(NSError *error) {
        NSLog(@"Error:%@", error);
    }];
   
}

-(void)creatTitleView
{
    self.hight = [KongWHightOfLabel labelFitHeight:self.content andFontSize:17];
    
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, kHeight * (self.hight + 50))];
    self.backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.backView];
    [self.backView release];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth * 10, 0, self.view.frame.size.width - kWidth * 20, kHeight * 50)];
    titleLabel.backgroundColor = [UIColor whiteColor];
    titleLabel.text = self.titleStr;
    titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:kWidth * 25];
    titleLabel.textColor = [UIColor colorWithRed:0.0 green:0.454 blue:1.0 alpha:1.0];
    [self.backView addSubview:titleLabel];
    [titleLabel release];
    
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth * 10, titleLabel.frame.origin.y + titleLabel.frame.size.height, self.view.frame.size.width - kWidth * 20, self.hight)];
    detailLabel.text = self.content;
    detailLabel.numberOfLines = 0;
    detailLabel.font = [UIFont systemFontOfSize:17];
    [self.backView addSubview:detailLabel];
    [detailLabel release];
    [self creatTableView];
}

-(void)creatTableView
{
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, self.backView.frame.origin.y + self.backView.frame.size.height + kHeight * 10, self.view.frame.size.width, self.view.frame.size.height - self.backView.frame.origin.y - self.backView.frame.size.height + kHeight * 25) style:UITableViewStyleGrouped];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.showsVerticalScrollIndicator = NO;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table registerClass:[KongWIllnessDetailTableViewCell class] forCellReuseIdentifier:@"illnessDetailCellIndentify"];
    
    [self.view addSubview:self.table];
    [self.table release];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHeight * 100;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KongWIllnessDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"illnessDetailCellIndentify"];
    cell.illnessDetailModel = [self.modelArray objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KongWCellSelectDetailViewController *cellSelectDetail = [[KongWCellSelectDetailViewController alloc] init];
    cellSelectDetail.uid = ((KongWIllnessDetailModel *)[self.modelArray objectAtIndex:indexPath.row]).ID;
    [self.navigationController pushViewController:cellSelectDetail animated:YES];
    [cellSelectDetail release];

}
-(void)backBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
